package com.qyhl.module_practice.study;

import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.SpecialDetailBean;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;

import java.util.List;

public class PracticeStudyModel implements PracticeStudyContract.PracticeStudyModel {
    private PracticeStudyPresenter mPresenter;

    PracticeStudyModel(PracticeStudyPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getSectionList(String sectionId, boolean isNew) {
        EasyHttp.get(PracticeUrl.GET_TOWN_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("type", isNew ? "0" : "1")
                .params("setions", sectionId)
                .execute(new SimpleCallBack<List<UnionBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("获取出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<UnionBean> unionBeans) {
                        if (unionBeans != null && unionBeans.size() > 0) {
                            mPresenter.setSection(unionBeans);
                        } else {
                            mPresenter.setError("暂无任何内容！");
                        }
                    }
                });
    }

    @Override
    public void getData(String sectionId) {

        EasyHttp.get(PracticeUrl.GET_SPECIAL)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("sections", sectionId)
                .execute(new SimpleCallBack<List<SpecialDetailBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("获取出错，请稍后再试！");
                        }
                    }

                    @Override
                    public void onSuccess(List<SpecialDetailBean> specialDetailBeans) {
                        if (specialDetailBeans != null && specialDetailBeans.size() > 0 && StringUtils.isNotEmpty(specialDetailBeans.get(0).getSection())) {
                            mPresenter.setData(specialDetailBeans);
                        } else {
                            mPresenter.setError("暂无任何内容！");
                        }
                    }
                });

    }
}
